package com.puhui.chome.util;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;

import com.puhui.chome.model.vo.Attachment;

public class SendEmail {

    public static void main(String[] args) {
        // 邮件内容
        String content = "测试邮件内容";

        EmailMessageInfo info = new EmailMessageInfo();
        info.setSubject("OA系统密码重置通知");
        info.setBody(content);
        info.setHtml(true);
        info.setFromAddress("chome-monitor@finupgroup.cn"); // 发件地址
        info.setFromName("CHome监控系统"); // 发件人
        info.addRecipient("dingxingang@finupgroup.com"); // 收件地址
        info.addCc("342652262@qq.com"); // 抄送
        // info.addBcc("lihao@puhuifinance.com"); // 密送
        // info.addAttachment("测试附件", "E://attach.txt");
        try {
            httpPostSendMail(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String smtp = "smtp.qiye.163.com"; // smtp主机
    private static String fromEmail = "chome-monitor@finupgroup.cn"; // 发件邮箱
    private static String fromPass = "chome@2017"; // 发件邮箱密码

    /**
     * 发送邮件
     * 
     * @param info
     * @return
     * @throws Exception
     */
    public static String httpPostSendMail(EmailMessageInfo info) throws Exception {
        Properties properties = new Properties();
        properties.put("mail.smtp.host", smtp);// 设置smtp主机
        properties.put("mail.smtp.auth", "true");// 使用smtp身份验证
        MimeMessage msg = new MimeMessage(Session.getInstance(properties, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, fromPass);
            }
        }));
        // 设置邮件的发件人
        msg.setFrom(new InternetAddress(fromEmail));
        // 设置邮件的收件人
        for (String email : info.getRecipients()) {
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        }
        // 查送 CC
        Address[] ccs = new Address[info.getCc().size()];
        for (int i = 0; i < info.getCc().size(); i++) {
            ccs[i] = new InternetAddress(info.getCc().get(i));
        }
        msg.setRecipients(Message.RecipientType.CC, ccs);
        // 密送 BCC
        Address[] bccs = new Address[info.getBcc().size()];
        for (int i = 0; i < info.getBcc().size(); i++) {
            bccs[i] = new InternetAddress(info.getBcc().get(i));
        }
        msg.setRecipients(Message.RecipientType.BCC, bccs);
        // 设置邮件的主题
        msg.setSubject(info.getSubject());
        // 创建邮件的正文
        MimeBodyPart text = new MimeBodyPart();
        text.setContent(info.getBody(), "text/html;charset=utf-8");
        // 关系 正文和图片的
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.setSubType("related");// 设置正文与图片之间的关系
        // 图片与正文的 body
        MimeBodyPart all = new MimeBodyPart();
        all.setContent(mm);

        // 附件与正文（text 和 img）的关系
        MimeMultipart mm2 = new MimeMultipart();
        mm2.addBodyPart(all);
        // 添加附件
        for (Attachment attach : info.getAttachments()) {
            // 新建一个存放附件的BodyPart
            BodyPart body = new MimeBodyPart();
            DataHandler dh = new DataHandler(new ByteArrayDataSource(attach.getData(), "application/octet-stream"));
            body.setDataHandler(dh);
            // 附件名称
            body.setFileName(MimeUtility.encodeText(attach.getAlias(), "utf-8", null));
            // 将含有附件的BodyPart加入到MimeMultipart对象中
            mm2.addBodyPart(body);
        }
        mm2.setSubType("mixed");// 设置正文与附件之间的关系

        msg.setContent(mm2);
        msg.saveChanges(); // 保存修改
        Transport.send(msg);// 发送邮件
        return "OK";
    }

}
